﻿using Games.Basic;
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;

namespace Games.Common
{
    public static class ControlUtil
    {
        public static ScrollView GetAbout(IGameInfo gameInfo)
        {
            var layout = new StackLayout
            {
                Padding = new Thickness(20, 5),
            };
            var startLayoutOptions = new LayoutOptions(LayoutAlignment.Start, false);
            layout.Children.Add(new Label
            {
                Text = gameInfo.Desc,
                TextColor = Color.Black,
                HorizontalTextAlignment = TextAlignment.Start,
                HorizontalOptions = startLayoutOptions,
                FontSize = 15,
            });
            layout.Children.Add(new Label
            {
                Text = $"版本：{gameInfo.Version}",
                TextColor = Color.Black,
                HorizontalTextAlignment = TextAlignment.Start,
                HorizontalOptions = startLayoutOptions,
                FontSize = 15,
            });
            var view = new ScrollView
            {
                Content = layout
            };
            return view;
        }
        public static (ScrollView, IDictionary<string, Button>) GetLevelGrid(List<string> list, int enableMax, int colCount, Action<string> action)
        {
            if(list == null || list.Count == 0)
            {
                throw new ArgumentNullException(nameof(list));
            }
            var grid = new Grid
            {
                Padding = new Thickness(20, 5),
            };
            if(enableMax < 0)
            {
                enableMax = list.Count;
            }
            if(colCount < 1)
            {
                colCount = 5;
            }
            if(colCount > 10)
            {
                colCount = 10;
            }
            var maxRow = list.Count / colCount + 1;
            for(int i = 0; i < colCount; i++)
            {
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Star });
            }
            for(int i = 0; i < maxRow; i++)
            {
                grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
            }
            var btns = new Dictionary<string, Button>();

            var index = 0;
            var layopt = new LayoutOptions(LayoutAlignment.Fill, true);
            var margin = new Thickness(1);
            foreach(var item in list)
            {
                var col = index % colCount;
                var row = index / colCount;
                var btn = new Button { Text = item, IsEnabled = index <= enableMax, Margin = margin, HorizontalOptions = layopt };
                grid.Children.Add(btn, col, row);
                btn.Clicked += (s, e) => action(item);
                btns.Add(item, btn);
                index++;
            }
            var view = new ScrollView
            {
                Content = grid
            };
            return (view, btns);
        }
        public static (ScrollView, List<Button>) GetLevelGrid(int max, int enableMax, int colCount, Action<int> action)
        {
            var list = Enumerable.Range(1, max).Select(x => x.ToString()).ToList();
            var (view, dic) = GetLevelGrid(list, enableMax, colCount, v => action(int.Parse(v)));
            var ndic = dic.Values.ToList();
            return (view, ndic);
        }
        public static Color BtnBackColor { get; }
        public static Color BtnDisableBackColor { get; } = Color.LightGray;
    }
}